package com.china08.yxyapi.api.home.kecheng.xiaoben;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.lessons.YxyLessonsSchoolLessonChapter;
import com.china08.yxyapi.entity.db.lessons.YxyLessonsSchoolLessonItem;
import com.china08.yxyapi.entity.db.lessons.YxyLessonsVideo;
import com.china08.yxyapi.model.YxyApiPageReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.YxyLessonsSchoolLessonChapterRepository;
import com.china08.yxyapi.repository.db.YxyLessonsSchoolLessonItemRepository;
import com.china08.yxyapi.repository.db.YxyLessonsVideoRepository;

/**
 * 校本课程--课时
 * 
 * @author LiuWeidong
 *
 */
@CrossOrigin
@RestController
public class KechengXiaobenLessonItemSrhController extends YxyApiBaseController {

    // 课时
    @Autowired
    private YxyLessonsSchoolLessonItemRepository yxyLessonsSchoolLessonItemRepository;

    // 章节
    @Autowired
    private YxyLessonsSchoolLessonChapterRepository yxyLessonsSchoolLessonChapterRepository;

    /** 视频 */
    @Autowired
    private YxyLessonsVideoRepository yxyLessonsVideoRepository;

    @RequestMapping(value = "/api_yw/home/kecheng/xiaoben/lesson/item/srh", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel find(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel reqModel) {

        String lessonItemId = reqModel.lessonItemId;
        YxyLessonsSchoolLessonItem yxyLessonsSchoolLessonItems = yxyLessonsSchoolLessonItemRepository
                .findById(lessonItemId);
        RespModel model = new RespModel();
        if (null != yxyLessonsSchoolLessonItems) {
            YxyLessonsSchoolLessonChapter schoolLessonChapter = yxyLessonsSchoolLessonChapterRepository
                    .findById(yxyLessonsSchoolLessonItems.getChapterId());

            YxyLessonsVideo yxyLessonsVideos = yxyLessonsVideoRepository
                    .findById(yxyLessonsSchoolLessonItems.getVideoId());
            if (null != yxyLessonsVideos) {
                model.duration = yxyLessonsVideos.getDuration();
            }

            model.chapter = schoolLessonChapter.getTitle();
            model.title = yxyLessonsSchoolLessonItems.getTitle();
            model.intro = yxyLessonsSchoolLessonItems.getIntro();
            model.orderNum = yxyLessonsSchoolLessonItems.getOrderNum();
        }

        return model;
    }

    /* 请求参数 */
    protected static class ReqModel extends YxyApiPageReqModel {

        // 课时ID
        public String lessonItemId;
    }

    /* 响应参数 */
    protected static class RespModel extends YxyApiRespModel {

        // 名称
        public String title;
        // 简介
        public String intro;
        // 章节
        public String chapter;
        // 视频时长
        public Integer duration;
        // 课时几
        public Integer orderNum;
    }
}
